//
//  CCommentary.m
//  HCHK_iphone
//
//  Created by yilong zhang on 11-12-13.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CCommentary.h"

@implementation CCommentary

@synthesize C_AID;
@synthesize P_AID;
@synthesize C_PubNick;
@synthesize C_PubID;
@synthesize C_Content;
@synthesize C_Date;
@synthesize C_PubIconUrl;

@synthesize m_bIsLoadIcon;
@synthesize m_ImageView;
@synthesize m_IconTask;

+(int)CreateCommentaryTable
{
    NSMutableArray *keys = [NSMutableArray array];
    
    CKey *key = [[CKey alloc] initWithType:1 Length:32 Name:@"C_AID" Primary:YES];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"P_AID" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"C_PubID" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"C_Date" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:32 Name:@"C_PubNick" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:280 Name:@"C_Content" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:150 Name:@"C_PubIconUrl" Primary:NO];
    [keys addObject:key];
    [key release];
    
    return [[CDBManager ScheduledShareInstance] CreateTable:@"Commentaries" Keys:keys];
}

+(int)GetCommsTotal:(NSString*)where
{    
    return [[CDBManager ScheduledShareInstance] GetRecordTotal:@"Commentaries" Condition:where Key:@"C_AID"];
}

+(NSArray*)GetLimitCommentaries:(NSString*)where Offset:(int)offset Limit:(int)limit
{
    NSArray         *keys  = [NSArray arrayWithObjects:@"*", nil];
    NSArray         *rets  = nil;
    NSMutableArray  *comms = [NSMutableArray array];
    CCommentary     *comm  = nil;
    
    rets = [[CDBManager ScheduledShareInstance] QueryRecordsFromCursor:@"Commentaries" Keys:keys Offset:offset Limit:limit Column:Com_Properties_Num Condition:where];
    
    if (rets && [rets count] > 0)
    {
        for (int i=0; i<[rets count]; i++)
        {
            comm = [[CCommentary alloc] init];
            [comm SetCommentaryWithArr:[rets objectAtIndex:i]];
            [comms addObject:comm];
            [comm release];
        }
        
        return comms;
    }
    
    return nil;
}

+(int)UpdateCommentary:(NSString*)where Keys:(NSArray*)keys
{
    return [[CDBManager ScheduledShareInstance] UpdateRecordInfo:@"Commentaries" Keys:keys Condition:where];
}

+(int)RemoveCommentary:(NSString*)where
{
    return [[CDBManager ScheduledShareInstance] DeleteRecord:@"Commentaries" Condition:where];
}

- (id)init
{
    self = [super init];
    if (self)
    {
        // Initialization code here.
        self.C_AID      = [NSNumber numberWithLong:0];
        self.P_AID      = [NSNumber numberWithLong:0];
        self.C_PubNick  = @"好吃客";
        self.C_PubID    = [NSNumber numberWithLong:0];
        self.C_Content  = @"好吃客，好时刻";
        self.C_Date     = [NSNumber numberWithLong:0];
        self.C_PubIconUrl=nil;
        
        self.m_IconTask = nil;
        m_ImageView = nil;
        m_bIsLoadIcon = NO;
    }
    
    return self;
}
-(void)dealloc
{
    m_bIsLoadIcon = YES;
    m_ImageView = nil;
    [[CDownLoadManager DefaultDownLoadManager] CancelTask:m_IconTask];
    self.m_IconTask = nil;
    
    [C_AID release];
    [C_Content release];
    [C_Date release];
    [C_PubID release];
    [C_PubNick release];
    [P_AID release];
    [C_PubIconUrl release];
    
    [super dealloc];
}

-(void)SetCommentaryWithProduct:(CProduct*)product
{
    self.P_AID      = product.P_AID;
    self.C_PubNick  = product.P_PubName;
    self.C_PubID    = product.P_Publisher;
    self.C_Content  = product.P_Caption;
    self.C_Date     = product.P_Date; 
    self.C_PubIconUrl=product.P_PubIconUrl;
}

-(void)SetCommentaryWithDic:(NSDictionary*)dic
{
    self.C_AID      = [NSNumber numberWithLong:[[dic objectForKey:@"c_id"] intValue]];
    self.P_AID      = [NSNumber numberWithLong:[[dic objectForKey:@"pic_id"] intValue]];
    self.C_PubNick  = [dic objectForKey:@"user_nicename"];
    self.C_PubID    = [NSNumber numberWithLong:[[dic objectForKey:@"author"] intValue]];
    self.C_Content  = [dic objectForKey:@"content"];
    self.C_Date     = [NSNumber numberWithLong:[[AppConfig DefaultAppConfig] DateToInt:[dic objectForKey:@"c_time"]]];
    self.C_PubIconUrl = [dic objectForKey:@"user_img"];
}

-(void)SetCommentaryWithArr:(NSArray*)com
{
    int nCnt = 0;
    id  objc = nil;
    
    objc = [com objectAtIndex:nCnt++];
    self.C_AID = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
    
    objc = [com objectAtIndex:nCnt++];
    self.P_AID = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
    
    objc = [com objectAtIndex:nCnt++];
    self.C_PubID = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
    
    objc = [com objectAtIndex:nCnt++];
    self.C_Date = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
    
    objc = [com objectAtIndex:nCnt++];
    self.C_PubNick = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
    
    objc = [com objectAtIndex:nCnt++];
    self.C_Content = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
    
    objc = [com objectAtIndex:nCnt++];
    self.C_PubIconUrl = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
}

-(int)SaveCommentary
{
    NSMutableArray *keys = [NSMutableArray array];
    CKey            *key = nil;
    
    key = [[CKey alloc] initWithType:1 Value:self.C_AID Name:@"C_AID"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Value:self.P_AID Name:@"P_AID"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Value:self.C_PubID Name:@"C_PubID"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Value:self.C_Date Name:@"C_Date"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.C_PubNick Name:@"C_PubNick"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.C_Content Name:@"C_Content"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.C_Content Name:@"C_PubIconUrl"];
    [keys addObject:key];
    [key release];
    
    return [[CDBManager ScheduledShareInstance] InsertNewRecordToTable:@"Commentaries" Keys:keys];
}

-(NSData*)GetComWriterIcon:(NSString*)path
{
    //NSLog(@"COM_PATH:%@", path);
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    if (nil == data)
    {
        [self DownComWriterIcon:C_PubIconUrl];
    }
    
    return data;
}

-(void)DownComWriterIcon:(NSString*)url
{
    //NSLog(@"%@", url);
    if (!m_bIsLoadIcon)
    {
        m_bIsLoadIcon = YES;
        
        self.m_IconTask = [[CDownLoadManager DefaultDownLoadManager] AddTask:url Sponsor:self PostData:nil TimeOut:10 Data:[NSNumber numberWithShort:0] MType:Get UserInfo:nil];
    }
}

-(void)SaveComWriterIcon:(NSString*)path Data:(NSData*)data
{
    [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
}

//delegate(opretion)
-(void)DidOperationFinished:(COperation *)op Data:(NSData *)data Other:(id)other UserInfo:(id)userinfo
{
    if (data)
    {
        NSDictionary *info = nil;
        if ([other isKindOfClass:[NSDictionary class]])
        {
            info = (NSDictionary*)other;
            other = [info objectForKey:@"other"];
        }
        else {;}
        
        //ICON
        if (0 == [other shortValue])
        {
            if (data)
            {
                [self SaveComWriterIcon:[NSString stringWithFormat:@"%@/%d.jpg", [AppConfig DefaultAppConfig].m_IConsPath, [C_PubID longValue]] Data:data];
                if (m_ImageView)
                {
                    m_ImageView.image = [UIImage imageWithData:data];
                    m_ImageView = nil;
                }
            }
            
            m_bIsLoadIcon = NO;
            self.m_IconTask = nil;
        }
    }
}

-(void)DidOperationError:(COperation *)op Error:(NSError *)error Other:(id)other UserInfo:(id)userinfo
{
    if (0 == [other shortValue])
    {
        //NSLog(@"\r\n\r\n获得品论作者头像错误\r\n\r\n");
        m_bIsLoadIcon = NO;
        self.m_IconTask = nil;
    }
}

-(void)DidOperationTimeOut:(COperation *)op Other:(id)other UserInfo:(id)userinfo
{
    if (0 == [other shortValue])
    {
        NSLog(@"\r\n\r\n获得品论作者头像超时\r\n\r\n");
        m_bIsLoadIcon = NO;
        self.m_IconTask = nil;
    }
}

@end
